<template>
    <div>
        <!-- 面包屑导航区 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card >
        <!-- 搜索与添加区域 -->
       
    <el-row :gutter='20'>
        <el-col :span="8">  
            <el-input placeholder="请输入内容"
                 clearable
             @clear="getuserList"
             >
              
            <el-button slot="append" icon="el-icon-search" 
            ></el-button>
            </el-input>
        </el-col>

        <el-col :span="4">
            <el-button type="primary" @click="addDialogVisible=true">添加用户</el-button>

        </el-col>
    </el-row>

    <!-- 用户列表区域 -->
    <el-table :data="userlist" border stripe >
        <el-table-column type="index"></el-table-column>
    
        <el-table-column label="姓名" prop="username"></el-table-column>
         <el-table-column label="邮箱" prop="email"></el-table-column>
          <el-table-column label="电话" prop="mobile"></el-table-column>
           <el-table-column label="角色" prop="role_name"></el-table-column>
           <el-table-column label="状态" >
               <template slot-scope="scope">

                   <!-- 显示数据 -->
                   <!-- {{scope.row}} -->

                   <el-switch v-model="scope.row.mg_state" @change="userStateChanged(scope.row)">
                    </el-switch>
               </template>
           </el-table-column>
           <el-table-column label="操作" width="180px">
               <template slot-scope="scope">
                   <!-- 修改按钮 -->
                   <el-button type="primary" icon="el-icon-edit" size='mini' @click='showEdialog(scope.row.id)'></el-button>
                   <!-- 删除按钮 -->
                   <el-button type="danger" icon="el-icon-delete" size='mini' @click='removeUserById(scope.row.id)'></el-button>
                   <!-- 分配角色按钮 -->
                        <el-tooltip  effect="dark" content="分配角色" placement="top" :enterable="false">
                        <el-button type="warning" icon="el-icon-setting" size='mini' @click="setRole(scope.row)">
                       </el-button>
                        </el-tooltip>
                   

               </template>

           </el-table-column>
         </el-table>

        </el-card>

        <!-- 添加用户对话框 -->
        <el-dialog
  title="添加用户"
  :visible.sync="addDialogVisible"
  width="50%" @close="addDialogClosed">
  <!-- 内容主体区域 -->

      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef"  label-width="70px">
  <el-form-item label="用户名" prop="username">
    <el-input v-model="addForm.username"></el-input>
  </el-form-item>
    <el-form-item label="密码" prop="password">
    <el-input v-model="addForm.password"></el-input>
  </el-form-item>
    <el-form-item label="邮箱" prop="email">
    <el-input v-model="addForm.email"></el-input>
  </el-form-item>
    <el-form-item label="手机号" prop="mobile">
    <el-input v-model="addForm.mobile"></el-input>
  </el-form-item>
      </el-form>

  <!-- 底部区域 -->
  <span slot="footer" class="dialog-footer">
    <el-button @click="addDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addUser">确 定</el-button>
  </span>
</el-dialog>
    </div>
</template>


<script>

export default{
    data(){
        return{
        userlist:[],
        addDialogVisible:false,
        addForm:{
            username:'',
            password:'',
            email:'',
            mobile:''

        },
        addFormRules:{
            username:[
                {required:true,message:'请输入用户名',trigger:'blur'},
                {min:3,max:8,message:'用户名长度在3-5之间',trigger:'blur'}
            ],
            password:[
                {required:true,message:'请输入密码',trigger:'blur'},
                {min:6,max:20,message:'密码长度在6-20之间',trigger:'blur'}
            ],
            email:[
                {required:true,message:'请输入邮箱',trigger:'blur'},
                {type:'email',message:'请输入正确的邮箱',trigger:'blur'}
            ],
            mobile:[
                {required:true,message:'请输入手机号',trigger:'blur'},
            ]
        }
        }
    },
    created(){
        this.getuserList()
    },
    methods:{
        async getuserList(){
           const {data:res}=await this.$http.get('users')
           if(res.meta.status!==200){
            return this.$message.error('获取用户列表失败')
           }
           this.userlist=res.data.users
        },
       async removeUserById(id){
         const confirmResult= await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        // console.log(res);
        .catch(err=>{
            return err
        })
        // 如果用户确认删除，则返回值为字符串 confirmm
        // 如果用户取消了删除，则返回值为字符串 cancel
        if(confirmResult!=='confirm'){
            return this.$message.info('已取消删除')
        }
        // console.log('确认了删除');

        // 有问题！！！ 删除失败！！！
       const {data:res}=await this.$http.delete('users/'+id)
       if(res.meta.status!==200){
        return this.$message.error('删除失败')
       }
        this.$message.success('删除成功')
        this.getuserList()

        },
       async userStateChanged(userinfo){
        const {data:res}=await this.$http.put(`users/${userinfo.id}/state/${userinfo.mg_state}`)
        // console.log(res);
        if(res.meta.status!==200){
            return this.$message.error('更新状态失败')
        }
        this.$message.success('更新状态成功')
       },
        addUser(){
        // console.log(this.addForm);
        this.$refs.addFormRef.validate(async valid=>{
            // console.log(valid);
            if(!valid) return 
            const {data:res}=await this.$http.post('users',this.addForm)
            console.log(res);
        })
       },
       addDialogClosed(){
        this.$refs.addFormRef.resetFields()
       }
    }
}
</script>


<style lang="less" scoped>
    



</style>